package com.wdciti.sccp.iot.tsdata.core.model;

import lombok.Data;
import lombok.NonNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author JINWEN
 */
@Data
public class Series {
    private static final String DOT = ".";
    @NonNull
    private String path;

    private String prefixPath;
    @NonNull
    private Measurement measurement;

    private List<Long> timeStamps;

    private Map<Long, Object> tsValues = new HashMap<>();


    public Series(@NonNull String path, @NonNull Measurement measurement) {
        this.path = path;
        this.measurement = measurement;
    }

    public Series(@NonNull String prefixPath, @NonNull String key, @NonNull DataType type) {
        if (prefixPath.endsWith(DOT)) {
            this.path = prefixPath + key;
        } else {
            this.path = prefixPath + DOT + key;
        }
        this.measurement = new Measurement(key, type);
    }

    public Series(@NonNull String path, @NonNull DataType type) {
        int i = path.lastIndexOf(DOT);
        String key = path.substring(i + 1);
        String prefix = path.substring(0, i);
        this.path = path;
        this.prefixPath = prefix;
        this.measurement = new Measurement(key, type);
    }

    public void putTsValue(long timestamp, Object value) {
        this.tsValues.put(timestamp, value);
    }

    public DataType getDataType() {
        return this.measurement.getType();
    }
}
